// SPDX-FileCopyrightText: Copyright 2025-2025 深圳市同心圆网络有限公司
// SPDX-License-Identifier: Apache-2.0

import { UserApiClient } from "$lib/api/proto/user_api.client";
import { UserMsgApiClient } from "$lib/api/proto/user_msg_api.client";

import { RoadmapApiClient } from "$lib/api/proto/roadmap_api.client";
import { RoadmapDataApiClient } from "$lib/api/proto/roadmap_data_api.client";
import { RoadmapDataStateApiClient } from "$lib/api/proto/roadmap_data_state_api.client";
import { RoadmapMemberApiClient } from "$lib/api/proto/roadmap_member_api.client";
import { RoadmapQaApiClient } from "$lib/api/proto/roadmap_qa_api.client";
import { RoadmapRagApiClient } from "$lib/api/proto/roadmap_rag_api.client";
import { RoadmapPlanApiClient } from "$lib/api/proto/roadmap_plan_api.client";

import { ResourceApiClient } from "$lib/api/proto/resource_api.client";
import { ResourceCardApiClient } from "$lib/api/proto/resource_card_api.client";
import { ResourceExamApiClient } from "$lib/api/proto/resource_exam_api.client";

import { GrpcWebFetchTransport } from "@protobuf-ts/grpcweb-transport";
import { uniqId } from "$lib/utils/id_utils";

export interface ToastMessage {
    id?: string;
    level: "info" | "success" | "warning" | "error";
    title?: string;
    content: string;
    keepTime: number; //保持提示时间，单位毫秒
}

export class AppStore {
    private grpcTrans = new GrpcWebFetchTransport({
        baseUrl: import.meta.env.VITE_GRPC_SERVER_URL,
        format: "text",
    });

    get userApiClient() {
        return new UserApiClient(this.grpcTrans);
    }

    get userMsgApiClient() {
        return new UserMsgApiClient(this.grpcTrans);
    }

    get roadmapApiClient() {
        return new RoadmapApiClient(this.grpcTrans);
    }

    get roadmapDataApiClient() {
        return new RoadmapDataApiClient(this.grpcTrans);
    }

    get roadmapDataStateApiClient() {
        return new RoadmapDataStateApiClient(this.grpcTrans);
    }

    get roadmapMemberApiClient() {
        return new RoadmapMemberApiClient(this.grpcTrans);
    }

    get roadmapQaApiClient() {
        return new RoadmapQaApiClient(this.grpcTrans);
    }

    get roadmapRagApiClient() {
        return new RoadmapRagApiClient(this.grpcTrans);
    }

    get roadmapPlanApiClient() {
        return new RoadmapPlanApiClient(this.grpcTrans);
    }

    get resourceApiClient() {
        return new ResourceApiClient(this.grpcTrans);
    }

    get resourceCardApiClient() {
        return new ResourceCardApiClient(this.grpcTrans);
    }

    get resourceExamApiClient() {
        return new ResourceExamApiClient(this.grpcTrans);
    }

    private _toastMessageList: ToastMessage[] = $state([]);

    get toastMessageList(): ToastMessage[] {
        return this._toastMessageList;
    }

    addToastMessage(msg: ToastMessage) {
        if (msg.id == undefined) {
            msg.id = uniqId(16)
        }
        this._toastMessageList.push(msg);
        setTimeout(() => {
            const tmpList = this._toastMessageList.filter(item => item.id != msg.id);
            this._toastMessageList = tmpList;
        }, msg.keepTime);
    }

}

export const appStore = $state(new AppStore());